﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace UmlDesigner.Model
{
    /// <summary>
    /// The DocumentSet is the master set of all documents currently open.
    /// It implements the singleton pattern so that it is globally accessable 
    /// from all places.
    /// </summary>
    [Serializable]
    internal class Project
    {
        #region SingletonPattern

        static internal Project Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Project();
                }
                return instance;
            }
        }

        static private Project instance = null;

        /// <summary>
        /// Private constructor to prevent construction via routes other than 
        /// getting the instance.
        /// </summary>
        private Project()
        {
        }

        #endregion

        internal List<Document> Documents
        {
            get
            {
                return documents;
            }
        }

        private List<Document> documents = new List<Document>();

        internal Document CreateNewDocument()
        {
            Document doc = new Document();
            documents.Add(doc);
            return doc;
        }

        internal void DeleteDocument(Document doc)
        {
            documents.Remove(doc);
        }

        internal void LoadDataModel(string filename)
        {
            documents = new List<Document>();
            FileStream stream = new FileStream(filename, FileMode.Open);
            ModelSerializer ms = new ModelSerializer();
            ms.Load(this, stream);            
        }

        internal Document LoadDocument(Stream stream)
        {
            Document d = new Document();
            ModelSerializer ms = new ModelSerializer();
            ms.Load(d, Storage.Open(stream));
            return d;
        }

        internal Document MergeLoadDocument(Document current, MemoryStream stream)
        {
            Document doc = LoadDocument(stream);
            current.MergeDocument(doc);
            return doc;
        }

        internal void SaveDataModel(FileStream stream)
        {
            ModelSerializer ms = new ModelSerializer();
            ms.Save(this, stream);

            foreach (Document item in documents)
            {
                item.isModified = false;
            }
        }

        internal void DeleteAllDocuments()
        {
            documents.Clear();
        }

        internal bool Modified
        {
            get
            {
                foreach (Document item in documents)
                {
                    if (item.isModified)
                    {
                        return true;
                    }
                }
                return false;
            }
        }
    }
}
